<template>
  <div>
    <h3>附件</h3>
    <el-container style="width:100%">
      <el-table
        :data="fileList"
        stripe
        style="width: 100%">
        <el-table-column
          prop="name"
          label="图标"
        >
          <template slot-scope="scope">

              <d2-icon-svg name="pdf" v-if="scope.row.name.endsWith('pdf')" style="width: 100%"></d2-icon-svg>
              <el-icon class="el-icon-document" v-else></el-icon>

          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="文件名"
        >
          <template slot-scope="scope">
            <el-link href="'/pdfview/'+scope.row.website_url" v-if="scope.row.name.endsWith('pdf')">
              {{ scope.row.name }}
            </el-link>           <el-link href="scope.row.website_url" v-else>
              {{ scope.row.name }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column
          prop="file_size"
          label="大小"
        >
        </el-table-column>
        <el-table-column
          prop="create_date"
          label="创建时间"
        >
        </el-table-column>
      </el-table>
  </el-container>
    <el-upload
      class="upload-demo"
      action="https://www.kaikong.cn/web/binary/upload_attachment"
      :on-preview="handlePreview" :on-success="onSuccess"
      :on-remove="handleRemove" drag
      multiple name="ufile"
      :data="{callback:null,model:model,id:id}"
      list-type="text">
      <el-button size="small" type="primary">点击上传</el-button>
      <div slot="tip" class="el-upload__tip">不超过500kb?</div>
    </el-upload>

  </div>
</template>

<script>
import { searchRead } from '../api/http'
import D2ContainerCard from '../../src/components/d2-container/components/d2-container-card'

export default {
  name: 'Attachment',
  components: { D2ContainerCard },
  props: {
    model: { type: String, default: '' },
    id: { type: Number, default: 0 }
  },
  data () {
    return {
      fileList: [],
      file_table_columns: [],
      tablePage: {
        currentPage: 1,
        pageSize: 25,
        totalResult: 0
      },
      result: {}
    }
  },
  mounted: function () {
    this.$nextTick(function () {
      this.get_more_data()
    })
  },
  methods: {
    get_more_data: function (offset, type) {
      let self = this
      this.loading = true
      searchRead('ir.attachment', {
        fields: ['create_date', 'description', 'file_size', 'index_content', 'name', 'type', 'url', 'website_url'],
        domain: [['res_model', '=', this.model], ['res_id', '=', this.id]],
        order: this.order,
        limit: this.tablePage.pageSize,
        offset: ((this.tablePage.currentPage - 1) * this.tablePage.pageSize) || 0
      }).then(function (response) {
        if (!response) {
          return
        }
        self.fileList = response.result
        self.tablePage.totalResult = self.result.count
        self.loading = false
        clearInterval(self.timeInterval)
      }, function (error) {
        console.error(error)
        self.loading = false
        clearInterval(self.timeInterval)
      })
    },
    handleRemove (file, fileList) {
      console.log(file, fileList)
    },
    handlePictureCardPreview (file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handlePreview (file) {
      console.log(file)
    },
    onSuccess (file) {
      this.$nextTick(function () {
        this.get_more_data()
      })
    }
  }
}

</script>

<style scoped>

</style>
